package com.vecharm.app.ui.smartlable

import android.content.res.Configuration
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.WindowManager
import com.flyco.tablayout.listener.OnTabSelectListener
import com.ling.sdk.ui.BaseFragment
import com.vecharm.app.R
import com.vecharm.app.bean.Goods
import com.vecharm.app.bean.GoodsBean
import com.vecharm.app.bean.IMAGES
import com.vecharm.app.bean.VIDEO
import com.vecharm.app.manager.GoodsManager
import com.vecharm.app.ui.BaseShowActivity
import com.vecharm.app.ui.web.WebFragment
import kotlinx.android.synthetic.main.activity_main.*


class SmartLabelActivity : BaseShowActivity() {
    override fun getLayoutId() = R.layout.activity_main


    private val callBack = {
        clear()
        onInitView()
    }


    override fun onInitView() {
        window.addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED)
        super.onInitView()

        GoodsManager.callBack = callBack
        start()

        loadViewsForGoods(GoodsBean.getList())
    }

    private val fragments = ArrayList<BaseFragment>()

    override fun onNetworkChange(isConnected: Boolean, netType: Int) {
        if (isConnected) start()
    }

    private fun start() {
        GoodsManager.start()
    }

    private fun clear() {

        val transaction = supportFragmentManager.beginTransaction()
        fragments.forEach {
            (it as? IAction)?.onCleanUp()
            transaction.detach(it)
            transaction.remove(it)
        }
        transaction.disallowAddToBackStack()
        transaction.commitNowAllowingStateLoss()
        supportFragmentManager.executePendingTransactions()
        fragments.clear()

        tab.setTabData(emptyArray())

    }


    private fun loadViewsForGoods(list: ArrayList<Goods>) {
        if (list.isEmpty()) return

        list.forEach {
            try {
                val fragment = getFragment(it)
                fragments.add(fragment)
                (fragment as? IAction)?.preload(it.showUrl)
            } catch (e: Exception) {
                Log.e("SessionTab", e.localizedMessage)
            }
        }

        setUpTab(list)

        loadMultipleRootFragment(R.id.fragment_container, 0, *fragments.toTypedArray())
        currentFragment = fragments[0]
        (currentFragment as? IAction)?.onStartPlay()
        tab.currentTab = 0
    }

    private fun setUpTab(list: ArrayList<Goods>) {

        val title = list.map { it.name ?: "" }

        tab.setTabData(title.toTypedArray())
        tab.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                switchTab(position)
            }

            override fun onTabReselect(position: Int) {}
        })
    }

    private var currentFragment: Fragment? = null
    private fun switchTab(index: Int) {
        (currentFragment as? IAction)?.onPausePlay()
        currentFragment = fragments[index]
        (currentFragment as? IAction)?.onStartPlay()

        showHideFragment(fragments[index])
    }


    private fun getFragment(goods: Goods): BaseFragment {
        return when (goods.type) {
            IMAGES -> DecentBannerFragment().setGoods(goods)
            VIDEO -> VideoFragment().setList(goods)
            else -> WebFragment().setList(goods)
        }
    }

    private fun BaseFragment.setList(goods: Goods): BaseFragment {
        val bundle = Bundle()
        bundle.putStringArrayList("list", goods.showUrl)
        arguments = bundle
        return this
    }

    private fun BaseFragment.setGoods(goods: Goods): BaseFragment {
        val bundle = Bundle()
        bundle.putSerializable("goods", goods)
        arguments = bundle
        return this
    }
}
